/*
 * $Id$
 */
package corejs.rt;

public class Obj extends Value {

   // -- Object.prototype
   public static final Value prototype = new Obj(null) ;
   
   static void _obj_init_prototype_ () {
      prototype.store("toString", new Invocable() {
         @Override
         public Value invoke(final ExecutionContext ec) {
            return new Str("[object Object]");
         }
      });
      
      prototype.store("valueOf", new Invocable() {
         @Override
         public Value invoke(final ExecutionContext ec) {
            return ec.get();
         }
      });
   }
   
   public Obj(final Value proto) {
      super(proto);
   }

   /**
    *
    * @return
    */
   public Primitive toPrimitive(final ExecutionContext ec) {
      return toPrimitive(ec, true);
   }
  
   /**
    *
    * @param ec
    * @param hintNumber
    * @return
    */
   public Primitive toPrimitive(final ExecutionContext ec, 
                                final boolean hintNumber) {    
      try {
         return toPrimitive(ec, hintNumber ? "valueOf" : "toString");
      }
      catch (final TypeError e) {
         return toPrimitive(ec, hintNumber ? "toString" : "valueOf");
      }       
   }
  
   /**
    *
    * @param methodName
    * @return
    */
   private Primitive toPrimitive(final ExecutionContext ec, 
                                 final String methodName) {
      return ec.invoke(this, methodName).cast(Primitive.class);
   }
   
   /**
    * spec: 9.2
    * http://www.ecma-international.org/ecma-262/5.1/#sec-9.2
    * @return
    */
   @Override
   public Bool toBool() {return Bool.TRUE;}

   @Override
   public Str typeof() {return Str.OBJECT;}

}
